package com.example.statistics.ui.market

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.MarketBean
import com.example.statistics.bean.MarketCityBean
import com.example.statistics.bean.MarketCustomerBean
import com.example.statistics.bean.MarketSales
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.messageDialog
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.ex.onSingleLongClicked
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import kotlinx.android.synthetic.main.list_item_common_display.*
import org.jetbrains.anko.startActivity

class MarketCustomerListActivity : BaseLoadingListActivity<MarketCustomerBean>() {

    var action: String? = ""

    override val title: String
        get() = "市场客户"

    override val url: String
        get() = UrlConfig.MARKET_CUSTOMER_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_common_display

    override val httpParams: MutableList<Pair<String, String>>
        get() {
            val params = mutableListOf<Pair<String, String>>()
            val city = intent.getStringExtra(Constants.CITY)
            if (!city.isNullOrBlank()) {
                params.add("city" to city)
            }
            val marketId = intent.getIntExtra(Constants.ID, 0)
            if (marketId != 0) {
                params.add("marketId" to marketId.toString())
            }
            return params
        }
    override val inputSearch: Boolean
        get() = true

    override val inputSearchFunc: (MarketCustomerBean, String) -> Boolean = { bean, str ->
        bean.city.contains(str) || bean.market.contains(str) || bean.customer.contains(str)
                || bean.shop.contains(str) || bean.product.contains(str)

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION)
        topBarRightImage {
            activityForResult<MarketCustomerAddActivity>() {
                refresh()
            }
        }
    }

    override fun itemViewConvert(
        adapter: SlimAdapter,
        injector: ViewInjector,
        bean: MarketCustomerBean,
        position: Int
    ) {
        val title = if (bean.area.endsWith("区"))
            bean.city.substring(0, bean.city.length - 1) + " " + bean.customer
        else {
            bean.area + " " + bean.customer
        }
        val status = if (bean.shop.isBlank()) "" else "(" + bean.shop + ")"
        injector.text(R.id.tv_title, title)
            .text(R.id.tv_status, status)
            .text(R.id.tv_subtitle, bean.product)
            .text(R.id.tv_bottom_right, if (bean.market == "其他") "" else bean.market)
            .onActionLongClicked(
                "编辑" to {
                    activityForResult<MarketCustomerAddActivity>(
                        Constants.DATA to bean,
                        Constants.ACTION to Constants.ACTION_EDIT
                    ) {
                        refresh()
                    }
                },
                "复制" to {
                    activityForResult<MarketCustomerAddActivity>(Constants.DATA to bean) {
                        refresh()
                    }
                },
                "删除" to {
                    messageDialog(msg = "是否删除客户${bean.customer}?") { _, _ ->
                        httpGet<Any>(UrlConfig.MARKET_CUSTOMER_DELETE, config = {
                            listParams = listOf("id" to bean.ID)
                        }) {
                            refresh()
                        }
                    }
                }
            )

    }

}
